//
// Created by hjie on 23-12-28.
//

#pragma once

#include <string>
#include <vector>
#include <map>
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/timestamp.h>
#include <libavutil/threadmessage.h>
#include <libavcodec/packet.h>
#include <libavfilter/avfilter.h>
#include <libavutil/mem.h>
#include <libavutil/opt.h>
#include <libavfilter/buffersrc.h>
#include <libavfilter/buffersink.h>
};

struct VideoFilterParams
{
    int m_format = {-1};
    std::string m_format_string;
    AVRational  m_timebase;
    AVRational  m_framerate;
    int m_width  = {0};
    int m_height = {0};
};

class FilterGraphContext
{
public:
    FilterGraphContext();
    ~FilterGraphContext();
    bool InitGraph();
    bool InitBufferCtxForVideo(const VideoFilterParams & config);
    bool InitBufferSinkForVideo();
    bool ConfigGraph();
    AVFilterGraph *   GetFilterGraph();
    AVFilterContext * GetFilterBufferCtx();
    AVFilterContext * GetFilterBufferSinkCtx();
    bool AddFilterCtx(AVFilterContext * filter_ctx);
private:
    AVFilterGraph * m_filter_graph;
    AVFilterContext * m_buffer_ctx;
    AVFilterContext * m_buffer_sink_ctx;
    std::map<std::string,AVFilterContext *> m_filter_ctx_map;
};